package com.taptech.doufu.ui.viewholder.personalcenter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.util.TimeUtil;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.NewNovelCreateListTosActivity;
import com.taptech.doufu.bean.MineAbstractBean;
import com.taptech.doufu.bean.NovelDraftBean;
import com.taptech.doufu.bean.NovelDraftDesBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.ui.view.drawcircle.BaseRecyclerViewViewHolder;
import com.taptech.doufu.bean.TipInfo;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.listener.ResultTipListener;
import com.taptech.doufu.services.NovelCreateServices;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.TipDialog;

import java.util.ArrayList;

/**
 * Created by longsiyang on 15/10/27.
 */
public class NovelSectionDraftRecyclerViewHolder extends BaseRecyclerViewViewHolder implements View.OnClickListener, HttpResponseListener
{


    Context mContext;
    RelativeLayout novelSectionLayout;
    TextView novelSectionTitle;
    TextView novelSectionUpdateTime;
    TextView novelSectionWordCount;
    TextView novelSectionDeleteBtn;

    View timeLayout;
    TextView sentTimeTv,cancelTv;
    ImageView draftImag;

    NovelDraftBean draftBean;
    NovelDraftDesBean draftDesBean;
    ArrayList<String> novelIds;

    public NovelSectionDraftRecyclerViewHolder(Context context, View itemView)
    {
        super(context, itemView);
        mContext = context;
        novelSectionLayout = (RelativeLayout) itemView.findViewById(R.id.section_back_layout);
        novelSectionLayout.setOnClickListener(this);
        novelSectionTitle = (TextView) itemView.findViewById(R.id.novel_create_list_item_title);
        novelSectionUpdateTime = (TextView) itemView.findViewById(R.id.novel_create_list_item_time);
        novelSectionDeleteBtn = (TextView) itemView.findViewById(R.id.section_delete_btn);
        novelSectionDeleteBtn.setOnClickListener(this);

        sentTimeTv = (TextView) itemView.findViewById(R.id.novel_create_draft_sent_time_tv);
        cancelTv = (TextView) itemView.findViewById(R.id.novel_create_draft_cancel_tv);
        timeLayout =  itemView.findViewById(R.id.novel_create_draft_time_layout);
        draftImag =  (ImageView) itemView.findViewById(R.id.novel_create_list_item_drafts);
    }
    public NovelSectionDraftRecyclerViewHolder(Context context, View itemView,ArrayList<String> novelIds)
    {
        super(context, itemView);
        this.novelIds=novelIds;
        mContext = context;
        novelSectionLayout = (RelativeLayout) itemView.findViewById(R.id.section_back_layout);
        novelSectionLayout.setOnClickListener(this);
        novelSectionTitle = (TextView) itemView.findViewById(R.id.novel_create_list_item_title);
        novelSectionUpdateTime = (TextView) itemView.findViewById(R.id.novel_create_list_item_time);
        novelSectionWordCount = (TextView) itemView.findViewById(R.id.section_word_count);
        novelSectionDeleteBtn = (TextView) itemView.findViewById(R.id.section_delete_btn);
        novelSectionDeleteBtn.setOnClickListener(this);

        sentTimeTv = (TextView) itemView.findViewById(R.id.novel_create_draft_sent_time_tv);
        cancelTv = (TextView) itemView.findViewById(R.id.novel_create_draft_cancel_tv);
        cancelTv.setOnClickListener(this);
        timeLayout =  itemView.findViewById(R.id.novel_create_draft_time_layout);
        draftImag =  (ImageView) itemView.findViewById(R.id.novel_create_list_item_drafts);
    }
    @Override
    public void setViewHolderChildViewContent(MineAbstractBean bean, int position)
    {
        draftBean = (NovelDraftBean) bean;
        if (draftBean == null)
        {
            return;
        }
        draftDesBean = draftBean.getDescription();
        if (draftDesBean == null)
        {
            return;
        }
        if (draftDesBean.getArticle_title() != null)
        {
            novelSectionTitle.setText(draftDesBean.getArticle_title());
        }
        if (draftBean.getUpdated_at() != null)
        {
            novelSectionUpdateTime.setText("更新:"+DiaobaoUtil.seconds2TimeString(DiaobaoUtil.Date2seconds(draftBean.getUpdated_at())));
        }
        if (draftBean.getContent() != null && draftBean.getContent().getText() != null){
//            novelSectionWordCount.setText(draftBean.getContent().getText().length() + " 字");
            novelSectionWordCount.setText(DiaobaoUtil.getStringSizeNoNull(draftBean.getContent().getText()) + " 字");
        }
        String timeAt = draftBean.getTimer_at();
       if( timeAt == null || "0".equals(timeAt)){
           timeLayout.setVisibility(View.GONE);
           draftImag.setImageResource(R.drawable.novel_drafts_tag);
       }else {
           timeLayout.setVisibility(View.VISIBLE);
           String sentTime =  TimeUtil.getDate(Long.parseLong(timeAt)*1000);
           sentTimeTv.setText("定时发布:" + sentTime);
           draftImag.setImageResource(R.drawable.novel_list_timing_img);
       }
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.section_back_layout:
                if (draftBean == null || draftDesBean == null)
                {
                    return;
                }
                NovelCreateServices.enterNovelDraftCreate(mContext, draftBean,novelIds,"");

                break;
            case R.id.section_delete_btn:
                TipInfo info = new TipInfo();

                info.setTipContent("是否删除存稿");
                info.setTipListener(tipListener);
                TipDialog dialog = new TipDialog(mContext, R.style.updateDialog, info);
                dialog.show();

                break;
            case R.id.novel_create_draft_cancel_tv:
                TipInfo tipInfo = new TipInfo();
                tipInfo.setTipContent("确定取消该章节定时发布吗？");
                tipInfo.setTipListener(cancelListener);
                TipDialog tipDialog = new TipDialog(mContext, R.style.updateDialog, tipInfo);
                tipDialog.show();
                break;
        }
    }

    private ResultTipListener tipListener = new ResultTipListener()
    {

        @Override
        public void resultBack(int flag, int type)
        {
            // TODO Auto-generated method stub
            if (flag == 1) {
                NovelCreateServices.getInstance().loadDelDrafts(NovelSectionDraftRecyclerViewHolder.this, draftBean.getId());
            }


            //NovelCreateServices.getInstance().deleteNovelSectionRequest(NovelSectionDraftRecyclerViewHolder.this, draftDesBean.getObject_id());

        }
    };
    private ResultTipListener cancelListener = new ResultTipListener()
    {

        @Override
        public void resultBack(int flag, int type)
        {
            if (flag == 1) {
                NovelCreateServices.getInstance().cancelTimeSent(NovelSectionDraftRecyclerViewHolder.this, draftBean.getId());
            }

        }
    };

    @Override
    public void handleResponse(int handleType, HttpResponseObject response)
    {
        switch (handleType)
        {
            case NovelCreateServices.HANDLE_TYPE_DEL_DRAFTS:
                if (response != null && response.getStatus() == Constant.STATUS_SUCCESS)
                {
                    if (mHandler != null)
                    {
//                        mHandler.sendEmptyMessage(NewNovelCreateListActivity.REFRESH_REQUEST_CODE);
                        mHandler.sendEmptyMessage(NewNovelCreateListTosActivity.REFRESH_REQUEST_CODE);
                    }
                }
                break;
            case NovelCreateServices.HANDLE_TYPE_CANCEL_TIMESENT:
                if (response != null && response.getStatus() == Constant.STATUS_SUCCESS)
                {
                    if (mHandler != null)
                    {
                        mHandler.sendEmptyMessage(NewNovelCreateListTosActivity.REFRESH_REQUEST_CODE);
                    }
                }
                break;
        }
    }
}